//
//  NSString+Extension.m
//  01-QQ聊天布局
//
//  Created by apple on 14-4-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (CGFloat)getStringHeightWithText:(NSString *)text font:(UIFont *)font viewWidth:(CGFloat)width {
    // 设置文字属性 要和label的一致
    NSDictionary *attrs = @{NSFontAttributeName :font};
    CGSize maxSize = CGSizeMake(width, MAXFLOAT);
    
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 计算文字占据的宽高
    CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    
    // 当你是把获得的高度来布局控件的View的高度的时候.size转化为ceilf(size.height)。
    return  ceilf(size.height);
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithAttributeText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize andLineSpacing:(int)lineSpacing
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:lineSpacing];//调整行间距
    
    NSDictionary *attrs = @{NSFontAttributeName : font,NSParagraphStyleAttributeName:paragraphStyle.copy};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
//获取字符串的高度
- (CGSize)sizeWithStringAndWidth:(CGFloat)width string:(NSString *)string font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 800)//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    return rect.size;
}

//获取当前时间戳有两种方法(以秒为单位)

+(NSString *)getNowTimeTimestamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    return timeSp;
    
}


//可以用的
+(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}

//得到当前时间之后N天的日期
+ (NSString *)getTimeAfterNowWithDay:(NSInteger)day
{
    
    
    NSDate *nowDate = [NSDate date];
    
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    
    NSDate *theDate;
    
    if(day!=0)
    {
        
        NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
        
        
        theDate = [nowDate initWithTimeIntervalSinceNow: +oneDay*day ];
        
        [dateFormatter stringFromDate:theDate];
        //or
        //theDate = [nowDate initWithTimeIntervalSinceNow: -oneDay*day ];
    }
    else
    {
        theDate = nowDate;
        [dateFormatter stringFromDate:theDate];
    }
    NSString *timeString =  [dateFormatter stringFromDate:theDate];
    return timeString;
}


/**
 *  判断是不是6位密码
 */
- (BOOL)isValidSixNumber
{
    static NSString *const regex = @"^\[0-9]{6}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}

/**
 *  判断是不是6-20位字母加数字密码
 */
- (BOOL)isValidLoginNumber{
    //    static NSString *const regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,12}";//必须数字加字母
    static NSString *const regex = @"^[0-9a-zA-Z]{6,20}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}
/**
*  校验身份证号码
*/
- (BOOL)isCorrect:(NSString *)IDNumber
{
    NSMutableArray *IDArray = [NSMutableArray array];
    // 遍历身份证字符串,存入数组中
    if (IDNumber.length<18) {
           return NO;
       }
    for (int i = 0; i < 18; i++) {
        NSRange range = NSMakeRange(i, 1);
        NSString *subString = [IDNumber substringWithRange:range];
        [IDArray addObject:subString];
    }
    // 系数数组
    NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
    // 余数数组
    NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];
    // 每一位身份证号码和对应系数相乘之后相加所得的和
    int sum = 0;
    for (int i = 0; i < 17; i++) {
        int coefficient = [coefficientArray[i] intValue];
        int ID = [IDArray[i] intValue];
        sum += coefficient * ID;
    }
    // 这个和除以11的余数对应的数
    NSString *str = remainderArray[(sum % 11)];
    // 身份证号码最后一位
    NSString *string = [IDNumber substringFromIndex:17];
    // 如果这个数字和身份证最后一位相同,则符合国家标准,返回YES
    if ([str isEqualToString:string]) {
        return YES;
    } else {
        return NO;
    }
}
/**
 *  判断是不是手机号
 */
- (BOOL)isValidMobileNumber
{
    static NSString *const regex = @"^(1)\\d{10}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}

/**
 *  判断是不是纯数字
 */
+ (BOOL)deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}


//判断是否为空字符串
+(BOOL)isEmptyText:(NSString*)text{
//    if ([text isEqual:[NSNull null]]) {
//        return YES;
//    }
//    else if ([text isKindOfClass:[NSNull class]])
//    {
//        return YES;
//    }
//    else if (text == nil){
//        return YES;
//    }
    
    if (text == nil || text == NULL) {
        return YES;
    }
    if ([text isKindOfClass:[NSNull class]]) {
        return YES;
    }
//    else if (text.length == 0){
//        return YES;
//    }
    return NO;
}
+ (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(CGFloat)font{
 CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
options:NSStringDrawingUsesLineFragmentOrigin
 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
 context:nil];
 return rect.size.width;
}
#pragma mark 一串字符在一行中正常显示所需要的宽度 method
+ (CGFloat)autoWidthWithString:(NSString *)string Font:(NSInteger)font {
    
    //大小
    CGSize boundRectSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    //绘制属性（字典）
    NSDictionary *fontDict = @{ NSFontAttributeName: [UIFont systemFontOfSize:font]};
    //调用方法,得到宽度
    CGFloat newFloat = [string boundingRectWithSize:boundRectSize
                                            options: NSStringDrawingUsesLineFragmentOrigin
                        | NSStringDrawingUsesFontLeading
                                         attributes:fontDict context:nil].size.width;
    return newFloat;
}
-(NSMutableAttributedString *)LineText:(NSString *)content{
    NSDictionary * attribtDic= @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
     NSMutableAttributedString* attribtStr = [[NSMutableAttributedString alloc]initWithString:content attributes:attribtDic];
    return attribtStr;
}
+(NSString*)getTheCorrectNum:(NSString*)tempString
{
   
    float   temFloat =[tempString floatValue];
    return [NSString stringWithFormat:@"%0.2f",temFloat];

}
+(NSString *)getDoubleValue:(double)doubleValue{
    NSString * dStr      = [NSString stringWithFormat:@"%f", doubleValue];
    NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:dStr];
    return [dn stringValue];
}
+(BOOL)isNUllString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]== 0) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]||[string isEqualToString:@"<null>"]||[string isEqualToString:@"null"]) {
        return YES;
    }
    return NO;
}
+ (NSString *)isChinese:(NSString *)str {
   NSString *newString = str;
   //遍历字符串中的字符
   for(int i=0; i< [str length];i++){
       int a = [str characterAtIndex:i];
       //汉字的处理
       if( a > 0x4e00 && a < 0x9fff)
       {
           NSString *oldString = [str substringWithRange:NSMakeRange(i, 1)];
           NSString *string = [oldString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
           newString = [newString stringByReplacingOccurrencesOfString:oldString withString:string];
       }
       //空格处理
       if ([newString containsString:@" "]) {
           newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

       }
     //如果需要处理其它特殊字符,在这里继续判断处理即可.
   }
   return newString;
}

+(NSString *)toJson:(NSDictionary *)dic{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
}
// 把数组或字典转成json
+ (NSString *)toJSONData:(id)theData{
    NSString * jsonString = @"";
    if (theData != nil) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:nil];
 
        if ([jsonData length] != 0){
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }
    return jsonString;
}
//将某个时间转化成 时间戳
+ (NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]*1000;
    
    return timeSp;
}

// 将某个时间戳转化成 时间
+ (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
    
}


//时间差之间比较
+ (NSString *)timeBeforeInfoWithString:(NSString *)firstTime secondTime:(NSString *)secondTime{
    
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";

    NSDate *date1 = [formatter dateFromString:firstTime];
    NSDate *date2 = [formatter dateFromString:secondTime];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 3.利用日历对象比较两个时间的差值
    NSDateComponents *cmps = [calendar components:type fromDate:date2 toDate:date1 options:0];
    // 4.输出结果
    if (cmps.year > 0) {
        return [NSString stringWithFormat:@"%@年",@(cmps.year)];
    }else if(cmps.month > 0){
        return [NSString stringWithFormat:@"%@月",@(cmps.month)];
    }else if(cmps.day > 0){
        return [NSString stringWithFormat:@"%@日",@(cmps.day)];
    }else{
        return [NSString stringWithFormat:@"1日"];
    }
}
//获得当前时间戳 格式你定
+ (NSString *)getNowTimeTimestamp:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSDate *confromTimespNow = [NSDate dateWithTimeIntervalSince1970:[datenow timeIntervalSince1970]];
    
    NSString *timeSp = [NSString stringWithFormat:@"%lld", (long long)([confromTimespNow timeIntervalSince1970]*1000)];
    
//    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[datenow timeIntervalSince1970]];
//    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//    NSLog(@"时间戳转时间   = %@",[formatter stringFromDate:confromTimespNow]);
    
    return timeSp;
    
}
+ (NSString *)getNowDateWithFormat:(NSString *)format{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate date];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
/*
 **判断选择的两个日期是否冲突**
 */
+ (NSInteger)compareDate:(NSString*)stareDate withDate:(NSString*)endDate withFormat:(NSString *)format{
    NSInteger aa;
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:format];
    NSDate *dta = nil;
    NSDate *dtb = nil;
    
    dta = [dateformater dateFromString:stareDate];
    dtb = [dateformater dateFromString:endDate];
    NSComparisonResult result = [dta compare:dtb];
    switch (result) {
        case NSOrderedSame:
        {
            aa = 0;
        }
            break;
        case NSOrderedAscending:
        {
            aa = 1;
        }
            break;
        case NSOrderedDescending:
        {
            aa = -1;
        }
            break;
            
        default:
            break;
    }
    
    return aa;
}

// 计算两个日期相差的天数
+(NSInteger)dateCompare:(NSString *)firstTime secondTime:(NSString *)secondTime{
    
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";

    NSDate *date1 = [formatter dateFromString:firstTime];
    NSDate *date2 = [formatter dateFromString:secondTime];
    
    
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDate = [date1 dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:date1]];

    NSDate *lastDate = [date2 dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:date2]];
    
    NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];


    NSInteger day = intevalTime / 60 / 60 / 24;



    return day;
}
/**获取两个日期的相隔天数*/
+ (NSInteger)getTimeDiffer:(NSString*)stareDate withDate:(NSString*)endDate withFormat:(NSString *)format{
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    [dateFomatter setDateFormat:k_DateFormat];
    
    NSDate *rentReminds = [dateFomatter dateFromString:stareDate];
    NSDate *current = [dateFomatter dateFromString:endDate];
    
    NSTimeInterval time = [rentReminds timeIntervalSinceDate:current];
    

    NSInteger days = time/(3600*24);

    return days;
    
}
/*
 1. 可以输入整数
 2. 可以输入一位和两位小数
 3. 最多能输入一个小数点
 4. 整数部分避免出现001，032，这种情况

 金额输入限制：整数部分7位数，最多保留两位小数
 */
+ (BOOL)validateNumberNegativePoint:(NSString*)number{
    NSString *stringRegex = @"^(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,6}(([.]\\d{0,2})?)))?";
    NSPredicate *num = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
    return [num evaluateWithObject:number];
}
+ (BOOL)numberAndLetter:(NSString*)text{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [text isEqualToString:filtered];
}
// 纯数字
+ (BOOL)numberOnly:(NSString*)text{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:kNUM] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [text isEqualToString:filtered];
}
#pragma mark -- 获取首字母
+(NSString *)getInitials:(NSString *)text{
    NSString *str = [NSMutableString stringWithString:text];
    CFStringTransform((__bridge CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)str, NULL, kCFStringTransformStripCombiningMarks, NO);
    return [[str uppercaseString]substringToIndex:1];
}
// 显示--
+ (NSString *)stringWithNull:(NSString *)text{
    if (StringIsEmpty(text)) {
        return @"-";
    }
    return text;
}
// 删除--
+ (NSString *)stringRemoveWithNull:(NSString *)text{
    if ([text isEqualToString:@"-"]) {
        return @"";
    }
    return text;
}
+(CGFloat)remarksHeight:(NSString *)text{
  
    if (![text isKindOfClass:[NSString class]]|| text == nil || text.length == 0 ) return 110;
    CGSize constraint = CGSizeMake(kScreenWidth - 100, CGFLOAT_MAX ) ;
    CGRect rect = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil] ;
    return ceil( rect.size.height + 100);
}
// url编码
+ (NSString *)urlEncoding:(NSString *)url{
    return [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
// url编码
+ (NSURL *)URLStrEncoding:(NSString *)url{
    return [NSURL URLWithString:[self urlEncoding:url]];
}

// 获取文字最佳宽度
+ (CGFloat)getLabelOptimumWidth:(UILabel *)label WithFont:(CGFloat)fontSize{
    CGSize size = [label.text sizeWithFont:[UIFont systemFontOfSize:fontSize]];
    return size.width;
}
+(void)callPhone:(NSString *)phone{
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
}
+(NSString *)toTen:(NSString *)str{
    
    NSNumber *number = @([str doubleValue]);
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setPositiveFormat:@"0.00"];
    formatter.maximumFractionDigits = 2;
    formatter.roundingMode = NSNumberFormatterRoundDown;
    return [formatter stringFromNumber:number];
    
}
+(NSString *)toOne:(NSString *)str{
    
    NSNumber *number = @([str doubleValue]);
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setPositiveFormat:@"0.0"];
    formatter.maximumFractionDigits = 1;
    formatter.roundingMode = NSNumberFormatterRoundDown;
    return [formatter stringFromNumber:number];
    
}
#pragma mark ----两个数相加------------
+ (NSString *)calculateByadding:(NSString *)number1 secondNumber:(NSString *)number2{
    if (StringIsEmpty(number1)) {
        number1 = @"0";
    }
    if (StringIsEmpty(number2)){
        number2 = @"0";
    }
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
    NSDecimalNumber *addingNum = [num1 decimalNumberByAdding:num2];
    return [addingNum stringValue];
}
#pragma mark ----两个数相减------------ number1 - number2
+(NSString *)calculateBySubtractingMinuend:(NSString *)number1 subtractorNumber:(NSString *)number2{
    if (StringIsEmpty(number1)) {
        number1 = @"0";
    }
    if (StringIsEmpty(number2)){
        number2 = @"0";
    }
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
    NSDecimalNumber *addingNum = [num1 decimalNumberBySubtracting:num2];
    return [addingNum stringValue];
    
}

#pragma mark ----两个数相乘------------
+ (NSString *)calculateByMultiplying:(NSString *)number1 secondNumber:(NSString *)number2{
    if (StringIsEmpty(number1)) {
        number1 = @"0";
    }
    if (StringIsEmpty(number2)){
        number2 = @"0";
    }
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
    NSDecimalNumber *multiplyingNum = [num1 decimalNumberByMultiplyingBy:num2];
    return [multiplyingNum stringValue];
    
}
#pragma mark ----两个数相除------------
+ (NSString *)calculateByDividingNumber:(NSString *)number1 secondNumber:(NSString *)number2
{
    if (StringIsEmpty(number1)) {
        number1 = @"0";
    }
    if (StringIsEmpty(number2)){
        number2 = @"0";
    }
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
    NSDecimalNumber *dividingNum = [num1 decimalNumberByDividingBy:num2];
    return [dividingNum stringValue];
    
}
// 获取向前/向后日期
+ (NSString *)getCustomDateFromDate:(NSDate *)date withMonth:(NSInteger)month{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *nextString = [dateFormatter stringFromDate:mDate];
    return nextString;
}
// 获取向前/向后日期
+ (NSString *)getCustomDateFromDate:(NSString *)date withDay:(NSInteger)day{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    

    NSDate *tempDate = [dateFormatter dateFromString:date];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:day];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:tempDate options:0];
   
    NSString *nextString = [dateFormatter stringFromDate:mDate];
    return nextString;
}
+(NSString *)getCollectionNameWithType:(NSInteger)type{
    /**款项类别 0租金 1租车押金 2购车首付,3购车月供,4购车尾款, 5平台返佣 6保险赔付*/
    switch (type) {
        case 0:
            return @"租金";
            break;
        case 1:
            return @"押金";
            break;
        case 2:
            return @"首付";
            break;
        case 3:
            return @"月供";
            break;
        case 4:
            return @"尾款";
            break;
        default:
            break;
    }
    
    return @"租金";
}
+(NSInteger)getCollectionTypeNameWithName:(NSString *)name{
    /**@[@"租金",@"租车押金",@"购车首付",@"购车月供",@"购车尾款",@"平台返佣",@"保险赔付",@"保险费",@"违约金",@"其他",@"车贷",@"介绍费",@"牌照费"]*/
    
    if ([name isEqualToString:@"租金"]) {
        return  0;
    }
    else  if ([name isEqualToString:@"租车押金"]) {
        return  1;
    }
    else  if ([name isEqualToString:@"购车首付"]) {
        return  2;
    }
    else  if ([name isEqualToString:@"购车月供"]) {
        return  3;
    }else  if ([name isEqualToString:@"购车尾款"]) {
        return  4;
    }
    else  if ([name isEqualToString:@"平台返佣"]) {
        return  5;
    }
    else  if ([name isEqualToString:@"保险赔付"]) {
        return  6;
    }
    else  if ([name isEqualToString:@"保险费"]) {
        return  7;
    }
    else  if ([name isEqualToString:@"违约金"]) {
        return  8;
    }
    else  if ([name isEqualToString:@"其他"]) {
        return  9;
    }
    else  if ([name isEqualToString:@"车贷"]) {
        return  10;
    }
    else  if ([name isEqualToString:@"介绍费"]) {
        return  11;
    }
    else  if ([name isEqualToString:@"牌照费"]) {
        return  12;
    }
    
    return 0;
}
+(NSString *)getCollectionNameWithFundType:(NSInteger)type{
    /**款项类别 0租金 1租车押金 2购车首付,3购车月供,4购车尾款, 5平台返佣 6保险赔付 7保险费 8违约金 9其他 10车贷 11介绍费 12牌照费*/
    
    if (type == 0) {
        return  @"租金";
    }
    else  if (type == 1) {
        return  @"租车押金";
    }
    else  if (type == 2) {
        return  @"购车首付";
    }
    else  if (type == 3) {
        return  @"购车月供";
    }
    else  if (type == 4) {
        return  @"购车尾款";
    }
    else  if (type == 5) {
        return  @"平台返佣";
    }
    else  if (type == 6) {
        return  @"保险赔付";
    }
    else  if (type == 7) {
        return  @"保险费";
    }
    else  if (type == 8) {
        return  @"违约金";
    }
    else  if (type == 9) {
        return  @"其他";
    }
    else  if (type == 10) {
        return  @"车贷";
    }
    else  if (type == 11) {
        return  @"介绍费";
    }
    else  if (type == 12) {
        return  @"牌照费";
    }
    return 0;
}
+(NSString *)getPaidNameWithFundType:(NSInteger)type{
    /**款项类别 0购车月供   1购车款(全款)   2保险费用  3年检费用  4租车押金退还 5签约佣金 6车辆维修 7保养费 8中介费 9牌照费  10租金 11违约金  12保险理赔款 13车贷*/
  
    if (type == 0) {
        return  @"车贷";
    }
    else  if (type == 1) {
        return  @"购车款(全款)";
    }
    else  if (type == 2) {
        return  @"保险费用";
    }
    else  if (type == 3) {
        return  @"年检费用";
    }
    else  if (type == 4) {
        return  @"租车押金退还";
    }
    else  if (type == 5) {
        return  @"签约佣金";
    }
    else  if (type == 6) {
        return  @"车辆维修";
    }
    else  if (type == 7) {
        return  @"保养费";
    }
    else  if (type == 8) {
        return  @"中介费";
    }
    else  if (type == 9) {
        return  @"牌照费";
    }
    else  if (type == 10) {
        return  @"租金";
    }
    else  if (type == 11) {
        return  @"违约金";
    }
    else  if (type == 12) {
        return  @"保险理赔款";
    }
    
    return  @"车贷";
    

}
+(NSInteger )getPaidFundTypeWithName:(NSString *)name{
    /**款项类别 0购车月供   1购车款(全款)   2保险费用  3年检费用  4租车押金退还 5签约佣金 6车辆维修 7保养费 8中介费 9牌照费  10租金 11违约金  12保险理赔款 13车贷*/
  
    if ([name isEqualToString:@"车贷"]) {
        return  0;
    }
    if ([name isEqualToString:@"购车款(全款)"]) {
        return  1;
    }
    if ([name isEqualToString:@"保险费用"]) {
        return  2;
    }
    if ([name isEqualToString:@"年检费用"]) {
        return  3;
    }
    if ([name isEqualToString:@"租车押金退还"]) {
        return  4;
    }
    if ([name isEqualToString:@"签约佣金"]) {
        return  5;
    }
    if ([name isEqualToString:@"车辆维修"]) {
        return  6;
    }
    else if ([name isEqualToString:@"保养费"]) {
        return  7;
    }
    else if ([name isEqualToString:@"中介费"]) {
        return  8;
    }
    else if ([name isEqualToString:@"牌照费"]) {
        return  9;
    }
    else if ([name isEqualToString:@"租金"]) {
        return  10;
    }
    else if ([name isEqualToString:@"违约金"]) {
        return  11;
    }
    else if ([name isEqualToString:@"保险理赔款"]) {
        return  12;
    }
    
    return  0;
    

}

/**
 * @brief 计算某两个时间差（得出秒数）
 */
- (long)numberOfSecondToTime:(NSString *)time2 FromTime:(NSString *)time1
{
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //结束时间
    NSDate *fromDate = [dateFormatter dateFromString:time1];
    //当前时间
    NSDate *ToDate = [dateFormatter dateFromString:time2];
    //得到相差秒数
    NSTimeInterval differTime= (long)[ToDate timeIntervalSinceDate:fromDate];
    
    return differTime;
}
// 获取本周的开始日期和结束日期
+ (NSArray *)getWeekBeginAndEndDate{
        
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    /**获取当前几个月*/
    NSInteger monthDay = [comp month];
    NSLog(@"%ld",monthDay);
    
    // 获取几天是几号
    NSInteger day = [comp day];
    NSLog(@"%ld----%ld",weekDay,day);
    
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1)
    {
        firstDiff = -6;
        lastDiff = 0;
    }
    else
    {
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

        
    return @[firstDay, lastDay];
}
// 获取本月的开始日期和结束如期
+ (NSArray *)getMonthBeginAndEndDate{

    NSDate *nowDate = [NSDate date];

    
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    //设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:nowDate]; //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
        
    }else {
        return @[@"",@""];
        
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
    
    return @[beginString,endString];
    
    
}
    
@end
